package com.example.democlient;

import java.util.ArrayList;
import java.util.List;

import com.baidu.mapapi.GeoPoint;
import com.baidu.mapapi.MKAddrInfo;
import com.baidu.mapapi.MKBusLineResult;
import com.baidu.mapapi.MKDrivingRouteResult;
import com.baidu.mapapi.MKPlanNode;
import com.baidu.mapapi.MKPoiResult;
import com.baidu.mapapi.MKRoute;
import com.baidu.mapapi.MKRoutePlan;
import com.baidu.mapapi.MKSearch;
import com.baidu.mapapi.MKSearchListener;
import com.baidu.mapapi.MKSuggestionResult;
import com.baidu.mapapi.MKTransitRouteResult;
import com.baidu.mapapi.MKWalkingRouteResult;
import com.example.democlient.model.Task;
import com.example.democlient.network.SocketService;
import com.example.democlient.network.serverpackets.SM_ROAD_DESIGN_REQUEST;

public class RoadDesign implements Runnable {
	private final MyApplication app;
	private final Object lock = new Object();
	private Task[] taskArray;
	private List<Integer> list;
	
	public RoadDesign(MyApplication app, GeoPoint selfPoint) {
		this.app = app;
		int size = app.roadDesignList.size();
		taskArray = new Task[size+1];
		Task currPointTask = new Task();
		currPointTask.setId(-1);
		currPointTask.setXaxis( selfPoint.getLatitudeE6()/ 1E6);
		currPointTask.setYaxis( selfPoint.getLongitudeE6()/ 1E6);
		taskArray[0] = currPointTask;
		for(int i=0;i<size;i++){
			taskArray[i+1] = app.roadDesignList.get(i);
		}
		list = new ArrayList<Integer>();
		System.out.println("实例化");
	}

	public synchronized void startRoadDesign(Task start,Task end) throws InterruptedException {

		final MKSearch myMKSearch = new MKSearch();
		final MKPlanNode pn = new MKPlanNode();
		final MKPlanNode pnNext = new MKPlanNode();
		pn.pt =  new GeoPoint((int) (start.getXaxis() * 1E6),
				(int) (start.getYaxis() * 1E6));
		pnNext.pt =  new GeoPoint((int) (end.getXaxis() * 1E6),
				(int) (end.getYaxis() * 1E6));
		myMKSearch.init(app.mBMapMan, new MKSearchListener() {
			@Override
			public void onGetWalkingRouteResult(
					MKWalkingRouteResult arg0, int arg1) {
			}

			@Override
			public void onGetTransitRouteResult(
					MKTransitRouteResult arg0, int arg1) {
			}

			@Override
			public void onGetSuggestionResult(MKSuggestionResult arg0,
					int arg1) {
			}

			@Override
			public void onGetRGCShareUrlResult(String arg0, int arg1) {
			}

			@Override
			public void onGetPoiResult(MKPoiResult arg0, int arg1,
					int arg2) {
			}

			@Override
			public void onGetDrivingRouteResult(MKDrivingRouteResult res, int error) {
				synchronized (lock) {
					int distanceM;
					if(res!=null){
						MKRoutePlan plan = res.getPlan(0);
						MKRoute route = plan.getRoute(0);
						if(route!=null){
							distanceM = route.getDistance();
						}else{
							distanceM = 0;
						}
					}else{
						distanceM=0;
					}

					list.add(distanceM);
					lock.notify();
				}
			}

			@Override
			public void onGetBusDetailResult(MKBusLineResult arg0,
					int arg1) {
			}

			@Override
			public void onGetAddrResult(MKAddrInfo arg0, int arg1) {
			}
		});
		myMKSearch.drivingSearch("", pn, "", pnNext);
		try {
			lock.wait();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}
	
	
	@Override
	public void run() {
		try {
			synchronized(lock){
				for(int i=0;i<taskArray.length;i++ ){
					for(int j=0;j<taskArray.length;j++){
						if(i!=j){
							startRoadDesign(taskArray[i],taskArray[j]);
						}else{
							list.add(0);
						}
					}
				}
			}
			System.out.println("距离阶乘为:"+list.size());
			SocketService.getInstance().sendPacket(new SM_ROAD_DESIGN_REQUEST(taskArray.length,taskArray,list), app);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
